package ca.jsbr.canvaswar.games.unit 
{
	import ca.jsbr.as2js_lib.display.animations.Tween;
	import ca.jsbr.as2js_lib.display.canvas.view.SpriteView;
	import ca.jsbr.as2js_lib.utils.geom.Point;
	import ca.jsbr.as2js_lib.utils.grid.ITile;
	import ca.jsbr.canvaswar.games.faction.Faction;
	import ca.jsbr.canvaswar.games.map.IMapObject;
	import ca.jsbr.canvaswar.games.map.Tile;
	/**
	 * ...
	 * @author jsbr [jsricard@lvlstudio.com]
	 */
	public class Unit implements IMapObject
	{
		static public const LEFT:String = "left";
		static public const RIGHT:String = "right";
		static public const UP:String = "up";
		static public const DOWN:String = "down";
		
		private var _orientation:String;
		private var _asset:SpriteView;
		private var _position:Point;
		private var _move:int;
		private var _faction:Faction;
		private var _tile:Tile;
		private var _offset:Point;
		
		public function Unit(asset:SpriteView) 
		{
			_asset = new SpriteView(asset);
			_position = new Point(0,0);
			_offset = new Point(0,0);
		}
		
		public function clone():void
		{
			
		}
		
		public function moveTo(path:Vector.<ITile>):void
		{
			if (!path.length)
				return;
			var end:Tile = path.pop();
			_tile.unit = null;
			end.unit = this;
			_tile = end;
			Tween.to(_asset, 0.2, { x:end.asset.x + _offset.x, y:end.asset.y + _offset.y, onComplete:moveTo, onCompleteParam:[path] } );
		}
		
		
		public function get tile():Tile 
		{
			return _tile;
		}
		
		public function set tile(value:Tile):void 
		{
			_tile = value;
		}
		

		public function get asset():SpriteView 
		{
			return _asset;
		}
		
		public function get position():Point 
		{
			return _position;
		}
		
		public function set position(value:Point):void 
		{
			_position = value;
		}
		
		public function get orientation():String 
		{
			return _orientation;
		}
		
		public function set orientation(value:String):void 
		{
			_asset.play(value);
			_orientation = value;
		}
		
		public function get move():int 
		{
			return _move;
		}
		
		public function set move(value:int):void 
		{
			_move = value;
		}
		
		public function get faction():Faction 
		{
			return _faction;
		}
		
		public function set faction(value:Faction):void 
		{
			_faction = value;
		}
		
		public function get offset():Point 
		{
			return _offset;
		}
		
		public function set offset(value:Point):void 
		{
			_offset = value;
		}
		
		public function destroy():void 
		{
			if(_tile)
				_tile.unit = null;
		}
		
	}

}